package com.example.menuvirtualclient.rule;

import java.util.Date;
import java.util.List;

import android.content.Context;
import android.util.Log;

import com.example.menuvirtualclient.core.*;
import com.example.menuvirtualclient.data.ISession;
import com.example.menuvirtualclient.data.SessionFactory;
import com.example.menuvirtualclient.repository.OrderDetailRepository;


public class OrderDetailRule {

	private ISession _session;
	private final OrderDetailRepository _repository;

	public OrderDetailRule(Context context) {
		_session = SessionFactory.GetSession(context);
		_repository = new OrderDetailRepository(context);
		Log.i("Info", "Paso por el context");
	}
	
	public OrderDetail GetEntity(String table) throws Exception {
		try {
			_session.Open();
			//return _repository.GetAll();
			return GetByTable(table);
		} 
		catch (Exception ex) {
			throw ex;
		}
		finally{
			_session.Close();
		}
	}

	public void saveOrderDetail(OrderDetail orderDetail) throws Exception{
		try {
			_session.Open();
			_repository.Insert(orderDetail);
		} 
		catch (Exception ex) {
			throw ex;
		}
		finally{
			_session.Close();
		}
	}
	
	public OrderDetail Get(int id) throws Exception{
		try {
			_session.Open();
			return _repository.Get(id);
		} 
		catch (Exception ex) {
			throw ex;
		}
		finally{
			_session.Close();
		}
	}
	
	public void Update(OrderDetail orderDetail) throws Exception{
		try {
			_session.Open();
			_repository.Update(orderDetail);
		} 
		catch (Exception ex) {
			throw ex;
		}
		finally{
			_session.Close();
		}
	}
	
	public void deleteOrderDetail(OrderDetail orderDetail) throws Exception{
		try {
			_session.Open();
			_repository.Delete(orderDetail);
		} 
		catch (Exception ex) {
			throw ex;
		}
		finally{
			_session.Close();
		}
	}
	
	public void deleteOrderDetail(int orderId, int dishId) throws Exception{
		try {
			_session.Open();
			_repository.Delete(orderId, dishId);
		} 
		catch (Exception ex) {
			throw ex;
		}
		finally{
			_session.Close();
		}
	}
	
	public void deleteOrderDetail(int detailId) throws Exception{
		try {
			_session.Open();
			_repository.Delete(detailId);
		} 
		catch (Exception ex) {
			throw ex;
		}
		finally{
			_session.Close();
		}
	}
	
	
	public List<OrderDetail> GetAllByOrder(int id) throws Exception{
		try {
			_session.Open();
			String query = "SELECT id, orderid, dishid, date, status, cookingPoint FROM Orders_Details WHERE orderid = ?";
			String[] args = new String[1];
			args[0] = Integer.toString(id);
			List<OrderDetail> od = _repository.QueryList(query,  args);
			
			return od;
		} 
		catch (Exception ex) {
			throw ex;
		}
		finally{
			_session.Close();
		}
	}
	
	private OrderDetail GetByTable(String table) {
		Order order = new Order(1, 12, new Date(), 1, 1);
		OrderDetail orderDetail = new OrderDetail();

		return orderDetail;
	}
	
}
